layui.define(['element', 'layer','upload', 'form', 'zmnSelect', 'zmnOmsSelect', '$http'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let $http = layui.$http;
    let layer = layui.layer;
    let zmnSelect = layui.zmnSelect;
    let zmnOmsSelect = layui.zmnOmsSelect;
    let upload = layui.upload;
    let CONF_BASE_URL = '/conf/attachment/orderVideo';
    let timestamp = new Date().getTime();

    /**
     * 判断是否是正整数
     * @param val {number/string} 字符串或数字
     * @returns {boolean} 返回是否是数字
     */
    function isNumber(val) {
        var reg = /^[0-9]\d*$/;//正整数
        return reg.test(val);
    }

    /**
     * 初始化页面事件
     */
    function initEvent() {
        // 监听保存按钮
        form.on('submit()', function (data) {
            if (!data.field.categId) {
                layer.msg("请选择视频类型！", {"icon": 2});
                return false;
            }
            if (!isNumber(data.field.sort)) {
                layer.msg("请输入正整数！", {"icon": 2});
                return false;
            }
            if (!data.field.servCategIds) {
                layer.msg("请选择服务分类！", {"icon": 2});
                return false;
            }

            if (!data.field.bizType) {
                layer.msg("请选择业务类型！", {"icon": 2});
                return false;
            }

            if (!data.field.mustUpload) {
                layer.msg("请选择是否必传！", {"icon": 2});
                return false;
            }
            var url = CONF_BASE_URL + '/save.action';
            $http.lockSave(url, data.field, data.elem).then(function (rsp) {
                layer.msg('操作成功', { icon: 1, time: 1000 }, function () {
                    parent.layer.closeAll();
                });
            }, (msg) => {
                layer.msg(msg);
            });
            return false
        });
    }
    /**
     * 初始化下拉框
     */
    function initSelect(){
        zmnSelect.selectlServBizUnits({
            bizTypeSelectId: 'bizType'
        }, {
            bizTypeSelectId: {
                selectedValues: dataVo.bizType
            }
        });

        zmnOmsSelect.selectConfOrderAttachmentCategory('categId', {
            tips: "全部",   //默认提示
            type: 2,
            isRadio:true,
            selectedValues: dataVo.categId === null ? null : dataVo.categId,
            page: {
                enable: true,
                remote:true,
                pageSize: 20
            },
            remote: {
                events: {
                    extendParams: function () {
                        return {
                            status: 2
                        }
                    }
                }
            }
        });

        zmnSelect.selectServCategory('servCategIds', {
            selectedValues: dataVo.servCategIds === null ? null : dataVo.servCategIds.split(","),
        });

        // 应用渠道
        zmnSelect.searchChannel('channelIds', {
            selectedValues: dataVo.channelIds === null ? null : dataVo.channelIds.split(","),
            remote: {
                events: {
                    extendParams: function () {
                        return {
                            "rank": 2, //渠道层级：Integer类型；1-查询一级渠道，2-查询二级渠道,null不限
                        }
                    }
                }
            },
            events: {
                changed: function (values) {
                }
            }
        });

    }
    /**
     * 描述: 视频上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadVideo;

    function uploaderVideo(elemId, valueId) {
        upload.render({
            elem: '#' + elemId,
            exts: 'avi|mp4',
            accept: 'video'
            , url: '/conf/attachment/orderVideo/uploader?timestamp=' + timestamp + '&elemId=' + elemId
            ,size: 10 * 1024
            , beforeSend: function (obj) {
                indexUploadVideo = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.statusBool) {
                    let data = res.data;
                    let style = "display:block !important; width: 100% !important; ;height: 130px !important;";
                    $("#videoValue").val(data);
                    $("#egVideoShow").attr("src", data);
                    $("#egVideoShow").attr("style", style)
                    $("#egVideoSrc").attr("style", "display:none !important")
                    layui.layer.close(indexUploadVideo);
                    dataVo.videoSrc = data;
                } else {
                    layer.msg(res.message);
                }
                layui.layer.close(indexUploadVideo);
            },error: function (){
                layer.msg("上传文件最大10M");
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
        let next = $("#" + elemId + "Value").next();
        let next1 = $(next).next();
        $(next).attr("style", "visibility:hidden !important");
        $(next1).attr("style", "visibility:hidden !important");
    }

    /**
     * 初始化页面
     */
    function initPage() {
        uploaderVideo("egVideoSrc", "egSrc");
        reLoadImage('egImgSrc', dataVo.egSrc);
        // 点击删除图片
        $(document).on('click', '.btn-delete', function () {
            let style = "display:none !important;";
            $("#egVideoShow").attr("src", '');
            $("#videoValue").val("");
            $("#egVideoShow").attr("style", style)
            $("#egVideoSrc").attr("style", "display:block !important")
        });
        // 点击放大
        $(document).on('click', '.btn-review', function () {
            var src = $("#videoValue").val();
            if (src && src!="") {
                //调整预览大小
                let imgHtml = "<video src='" + src + "' id='preview-channel' style='width:96%;height:90%;margin:2%;'";
                imgHtml += "autoplay=‘autoplay’  controls='controls'/>";
                //弹出层
                layer.open({
                    type: 1,
                    shade: 0.8,
                    offset: 'auto',
                    area: [600 + 'px', 300 + 'px'],
                    shadeClose: true,
                    scrollbar: false,
                    title: "", //不显示标题
                    content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                    cancel: function () {
                        //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
                    }
                });
            }
        });

    }

    function init() {
        initEvent();
        initSelect();
        initPage();
    }

    init();
    exports('oms_conf_attachment_orderVideo_detail');
});

